/*
 * Copyright 2008 Neil Bacon
 * Distributed under the terms of the Mozilla Public License version 1.1
 * or the GNU General Public License version 3.
 */

package nb.xadd.node.impl;

import nb.util.Utils;
import nb.xadd.node.Node;

public abstract class AbstractRealDecisionNode extends AbstractDecisionNode {
    private final int realVarIndex;
    private final double testValue;

    protected AbstractRealDecisionNode(int realVarIndex, double testValue, Node trueChild, Node falseChild) {
        super(trueChild, falseChild);
        this.realVarIndex = realVarIndex;
        this.testValue = testValue;
    }

    public int getRealVarIndex() {
        return realVarIndex;
    }

    public double getTestValue() {
        return testValue;
    }

    @Override
    public int compareTo(Node other) {
        int i = super.compareTo(other);
        if (i != 0) {
            return i;
        }
        AbstractRealDecisionNode n = (AbstractRealDecisionNode) other;
        i = realVarIndex - n.realVarIndex;
        return i != 0 ? i : Double.compare(testValue, n.testValue);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!super.equals(obj)) {
            return false;
        }
        AbstractRealDecisionNode other = (AbstractRealDecisionNode) obj;
        return realVarIndex == other.realVarIndex && testValue == other.testValue;
    }

    protected int calcHashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + super.calcHashCode();
        result = prime * result + realVarIndex;
        result = prime * result + Utils.hashCode(testValue);
        return result;
    }

    @Override
    public void appendTo(int depth, StringBuilder buf) {
        Utils.appendIndent(depth, buf);
        buf.append("id = ").append(getId()).append(", x").append(realVarIndex).append(getSymbol()).append(testValue).append(" ?\n");
        super.appendTo(depth, buf);
    }

    @Override
    public String description() {
        return "id = " + getId() + ", x" + realVarIndex + getSymbol() + testValue + " ?";
    }

    /**
     * BinaryOperator symbol used in {@link #toString()} and {@link #appendTo(int, StringBuilder)}.
     */
    abstract String getSymbol(); 
}
